package com.controller;

import java.lang.reflect.InvocationTargetException;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.ModelAndView;

import com.beans.Code;
import com.vo.Page;

public class CodeController extends UniversalController {

	public ModelAndView getCodeList(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		Map map = new HashMap();
		Code c = new Code();
		bind(request, c);
		if (c.getCode() != null && !"".equals(c.getCode()))
			map.put("code", c.getCode());
		if (c.getpCode() != null && !"".equals(c.getpCode()))
			map.put("pCode", c.getpCode());
		if (c.getName() != null && !"".equals(c.getName()))
			map.put("code", "%" + c.getName() + "%");
		if (c.getType() != null && !"".equals(c.getType()))
			map.put("type", c.getType());
		Page page = getUniversalManager().findPageByCriteria(
				this.getPageNo(request), pageSize, map, Code.class);
		map.put("o", page);
		return new ModelAndView("admin/codeList", map);
	}

	public ModelAndView getCodeEdit(HttpServletRequest request,
			HttpServletResponse response) {
		Map map = new HashMap();
		String id = request.getParameter("id");
		if (id == null)
			return new ModelAndView("admin/codeEdit");
		try {
			Code c = (Code) getUniversalManager().findByID(Code.class,
					Integer.parseInt(id));
			map.put("o", c);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return new ModelAndView("admin/codeEdit", map);
	}
}
